#ifdef SYPX_MEMCHECK
#include "mmgr.h"
#endif

#include "SheepSeg.h"
#include "WolfSeg.h"

namespace SheepDefence
{
	SheepSeg::SheepSeg()
	{
	}

	SheepSeg::~SheepSeg()
	{
		delete x;
	}

	SheepSeg::SheepSeg(SypX::Font* f, TutorialScene* tut, SypX::Mesh* xM, SypX::Mesh* bgMesh, SypX::Mesh* cMesh, SypX::MeshInstance* xI, SypX::MeshInstance* bg, SypX::MeshInstance* c, SypX::TextBox* box, std::vector<Actor*>& act, std::vector<Instance*>& inst, DogInstance* d, std::vector<ObstacleInstance*>& obs, Graph* envir) : tutorial(tut), congrat(false), Level()
	{
		font = f;
		xMesh = xM;
		backgroundMesh = bgMesh;
		clockMesh = cMesh;
		x = xI;
		background = bg;
		clock = c;
		briefBox = box;
		actors = act;
		instances = inst;
		dog = d;
		obstacles = obs;
		environment = envir; 

		x->setPosition(SypX::Vec3(160.0f, 120.0f, 0.1f));
		
		std::vector<Instance*>::iterator i = instances.begin();

		while (i != instances.end())
		{
			(*i)->updateLevel(this);
			i++;
		}

		environment->updateLevel(this);

		Actor* currActor = Actor::loadActor("Actors/baabaa.act");
		actors.push_back(currActor);

		Instance* currInstance;
		currInstance = new SheepInstance(static_cast<Sheep*>(currActor), this, 80.0f, 400.0f, 6);
		sheep.push_back(static_cast<SheepInstance*>(currInstance));
		instances.push_back(currInstance);

		briefBox->setText("Good job at getting your dog here. Now, try to move the sheep to the big red cross by nudging it with your dog. Sheep are naturally afraid of dogs and will move away when your dog approaches.");
		changeState(BRIEFING);
	}

	void SheepSeg::advance(float time)
	{
		Level::advance(time);
		
		if (sheep[0]->x > 150.0f && sheep[0]->x < 170.0f && sheep[0]->y > 110.0f && sheep[0]->y < 130.0f)
			tutorial->level = new WolfSeg(font, tutorial, xMesh, backgroundMesh, clockMesh, x, background, clock, briefBox, actors, instances, dog, obstacles, environment, sheep);
	}

	void SheepSeg::draw()
	{
		Level::draw();
		SypX::Engine::getSingleton()->getRenderer()->drawMesh(x);
	}
}